﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StarSpawn : MonoBehaviour {

	public static StarSpawn instance;

	public GameObject starPrefab;//星球预设体
	public GameObject shieldPrefab;//防护罩预设体
	private GameObject gObj;//当前实例化的星球

	public Sprite starSprite;
	private Color starColor = Color.black;


	// 四个难度属性一开始有玩家设定好，这四个不要动，默认为5
	private  int starDensity;//星球密度(星球数量，星球间的最小距离, 产生星球区域的边长)
	private  int starPower;//星球技术力（卫星强度，护盾血量，星球护甲，星球血量，搜索范围，出兵速度，出兵种类和数量）
	private  int producePower;//造船技术力（速度，血量，防御）
	private  int planePower;//兵装技术力（敌机武器）

	private float R = 100;//星球的产生中心点与一开始玩家的距离
	private Vector3 spawnCenterPosition;//星球产生中心点
	private float minDistance = 20;//星球间最小距离
	private float sideDistance = 100;//产生星球区域的边长
	private List<Vector3> starTransforms = new List<Vector3>();//储存每个星球的位置
	private int starNum;//星球数量
	private int currStarNum;//当前星球数量


	private int i = 0;

	void Awake()
	{
		instance = this;
	}

	void Start () {
		InstanceStars();
	}
	
	void Update () {
		
	}

	//设置难度属性
	public void SetDiffcultPro(int starDensity, int starPower, int producePower, int planePower, int starNum){
		this.starDensity = starDensity;
		this.starPower = starPower;
		this.producePower = producePower;
		this.planePower = planePower;
		this.starNum = starNum;
	}


	// 实例化所有星球
	void InstanceStars(){
		spawnCenterPosition = InitSpawnCenterPosition();
		for (int i = 0; i < starNum; i++)
		{
			CreateStar();
		}
	}

	// 随机产生星球产生的中心点
	private Vector3 InitSpawnCenterPosition(){
		float x = Random.Range(-R, R);
		float y = Mathf.Sqrt(R*R - x*x);
		if(Random.value < 0.5){
			y = -y;
		}
		return new Vector3(x, y, 0);
	}

	// 实例化一个星球并设置属性
	void CreateStar(){
		gObj = GameObject.Instantiate(starPrefab, GetRandomPosition(spawnCenterPosition, minDistance, sideDistance), transform.rotation);
		gObj.GetComponent<Star>().SetPower(starPower, producePower, planePower);
		gObj.GetComponent<Star>().SetPrefab_body(starSprite);
		// gObj.GetComponent<Star>().SetColor(starColor);
		// CreatShield(1, gObj);


	}

	// 在一定范围内随机产生一个点，这个点与一个集合里面的所有点都有一个最小距离，并把这个点存入集合
	private Vector3 GetRandomPosition(Vector3 targetPosition, float minDistance, float sideDistance){
		Vector3 target = targetPosition;
		float min = minDistance;
		float side = sideDistance;
		float x = Random.Range((targetPosition.x - sideDistance / 2), (targetPosition.x + sideDistance / 2));
		float y = Random.Range((targetPosition.y - sideDistance / 2), (targetPosition.y + sideDistance / 2));
		Vector3 randomPosition = new Vector3(x, y, 0);

		if(starTransforms.Count > 0){
			foreach (Vector3 position in starTransforms)
			{
				if(Vector3.Distance(randomPosition, position) < minDistance){
					return GetRandomPosition(target, min, side);
				}
			}
			starTransforms.Add(randomPosition);
			return randomPosition;
		}else{
			starTransforms.Add(randomPosition);
			return randomPosition;
		}
	}

	
	private GameObject CreatShield(int grade, GameObject target)
	{
		GameObject shield = GameObject.Instantiate(shieldPrefab);
		shield.transform.position = new Vector3(target.transform.position.x, target.transform.position.y, 0);
		shield.transform.rotation = transform.rotation;
		shield.GetComponent<Shield>().SetTarget(target);
		shield.transform.localScale = new Vector3(2f, 2f, 0);
		return shield;
	}
	  
}
